//
// Created by root on 24-7-21.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>

#include "./include/readfile.h"
void sendplain(int fd) {
    char send_buffer[1024];
    char *response_line = "Http/1.1 200 OK\r\n";
    char *response_head = "Content-type: text/plain\r\n";
    char *response_head2 = "Content-length: 16\r\n";
    char *response_head_over = "\r\n";
    char *response_body = "hello jihaosheng";
    sprintf(send_buffer, "%s%s%s%s%s", response_line, response_head, response_head2, response_head_over, response_body);
    send(fd, send_buffer, strlen(send_buffer), 0);
};

int sendhtml(int fd,char *path,char * mode) {
    struct fileinfo info = getfiledata_need_free(path,mode);
    char *response_line = "Http/1.1 200 OK\r\n";
    char *response_head = "Content-type: text/html\r\n";

    char response_head2[64];
    sprintf(response_head2, "%s %ld\r\n", "Content-length:", info.filelen);

    char *response_head_over = "\r\n";

    char sendbuffer[1024] ;
    sprintf(sendbuffer, "%s%s%s%s",response_line,response_head,response_head2,response_head_over);
    if(send(fd,sendbuffer,strlen(sendbuffer),0)==-1) {
        free(info.data_needfree);
        fclose(info.fd);
        return -1;
    };
    if(send(fd,info.data_needfree,info.filelen,0)==-1) {
        free(info.data_needfree);
        fclose(info.fd);
        return -1;
    };
    free(info.data_needfree);
    fclose(info.fd);
    return 1;
}
